<template>
  <div class="employee-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">企业名称:</div>
      <el-input
        clearable
        placeholder="请输入内容"
        class="search-main"
        v-model="params.enterpriseName"
        @clear="search"
      />
      <div class="search-label">缴费时间:</div>
      <el-date-picker
        v-model="valuedata"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        format="yyyy-MM-dd"
        value-format="yyyy-MM-dd"
        @blur="search"
      >
      </el-date-picker>
      <el-button type="primary" @click="search">查询</el-button>
    </div>
    <div class="create-container">
      <el-button type="primary" @click="dialogVisible = true"
        >添加账单</el-button
      >
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="feeList">
        <el-table-column type="index" label="序号" :index="indexMethod" />
        <el-table-column label="账单编号" width="180" prop="billNumber" />
        <el-table-column label="企业名称" width="250" prop="enterpriseName" />
        <el-table-column label="租界楼宇" width="120" prop="buildingName" />
        <el-table-column label="物业费(元/m²)" prop="propertyFeePrice" />
        <el-table-column label="账单金额(元)" prop="paymentAmount" />
        <el-table-column label="缴费时间" prop="createTime" />
        <el-table-column label="操作" fixed="right" width="120">
          <template #default="scope">
            <el-button size="mini" type="text" @click="read(scope.row)"
              >查看</el-button
            >
            <el-button size="mini" type="text" @click="del(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="page-container">
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <!-- 添加账单 -->
    <el-dialog title="添加账单" width="600px" :visible="dialogVisible">
      <!-- 表单接口 -->
      <div class="form-container">
        <el-form
          ref="addForm"
          :model="addForm"
          :rules="rulesForm"
          label-width="80px"
        >
          <el-form-item label="选择租户" prop="enterpriseId">
            <el-select placeholder="请选择租户" v-model="addForm.enterpriseId">
              <el-option
                v-for="item in enterpriseList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="租赁楼宇" prop="buildingId">
            <el-select
              placeholder="请选择租赁楼宇"
              v-model="addForm.buildingId"
            >
              <el-option
                v-for="item in buildingList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="缴费周期" prop="payTime">
            <el-date-picker
              v-model="addForm.payTime"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
          <el-form-item label="支付金额" prop="paymentAmount">
            <el-input
              v-model="addForm.paymentAmount"
              placeholder="请输入支付金额"
              :disabled="true"
            ></el-input>
          </el-form-item>
          <el-form-item label="支付方式" prop="paymentMethod">
            <el-select
              placeholder="请选择支付方式"
              v-model="addForm.paymentMethod"
            >
              <el-option
                v-for="item in payList"
                :key="item.value"
                :label="item.text"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="submit">确 定</el-button>
      </template>
    </el-dialog>

    <!-- 查看账单 -->
    <el-dialog title="查看账单" width="600px" :visible="readShow">
      <!-- 表单接口 -->
      <div class="form-container">
        <p>
          租户名称：<span>{{ readData.enterpriseName }}</span>
        </p>
        <p>
          租赁楼宇：<span>{{ readData.buildingName }}</span>
        </p>
        <p>缴费周期：<span></span></p>
        <p>物业费(元/m²)：</p>
        <p>账单金额(元)：</p>
        <p>支付方式：</p>
        <p>缴费时间：</p>
      </div>
      <template #footer>
        <el-button size="mini" @click="readShow = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="readShow = false"
          >确 定</el-button
        >
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  getPropertyfeeListAPI,
  getPaymentAPI,
  addPaymentAPI,
  delPaymentAPI
} from '@/api/propertyfee'
import { getAllEnterpriseAPI } from '@/api/enterprise'
import { getAllBuildingAPI } from '@/api/building'
export default {
  name: 'PropetyPage',
  data () {
    return {
      // 请求参数
      params: {
        page: 1, // 页码
        pageSize: 10, // 页容量
        enterpriseName: '', // 企业名称
        start: '', // 开始时间
        end: '' // 结束时间
      },
      feeList: [], // 企业物业费列表数据
      total: 0, // 总条数
      valuedata: '', // 时间
      // 添加账单数据
      addForm: {
        enterpriseId: '', // 企业id
        // enterpriseName: '', // 企业名称
        buildingId: '', // 楼宇id
        // buildingName: '', // 楼宇名称
        payTime: '', // 缴费周期
        startTime: '', // 开始时间
        endTime: '', // 结束时间
        paymentAmount: '', // 付款总额
        paymentMethod: '' // 支付方式
      },
      // 表单校验规则
      rulesForm: {
        enterpriseId: [
          { required: true, message: '请选择租户', trigger: 'change' }
        ],
        buildingId: [
          { required: true, message: '请选择租赁楼宇', trigger: 'change' }
        ],
        payTime: [
          { required: true, message: '请选择缴费周期', trigger: 'change' }
        ],
        paymentAmount: [
          { required: true, message: '请输入支付金额', trigger: 'blur' }
        ],
        paymentMethod: [
          { required: true, message: '请输入支付方式', trigger: 'change' }
        ]
      },
      // 支付方式
      payList: [
        { value: 1, text: '微信' },
        { value: 2, text: '支付宝' },
        { value: 3, text: '现金' }
      ],
      dialogVisible: false,
      readShow: false,
      enterpriseList: [], // 企业列表
      buildingList: [], // 楼宇列表
      readData: []
    }
  },
  created () {
    this.getPropertyfeeList() // 获取物业列表
    this.getAllEnterprise() // 获取所有企业
    this.getAllBuilding() // 获取所有楼宇
  },
  methods: {
    // 获取物业费列表
    async getPropertyfeeList () {
      const res = await getPropertyfeeListAPI(this.params)
      this.feeList = res.data.rows
      this.total = res.data.total
      // console.log('企业物业费列表数据', res.data.rows)
    },
    // 页码发生改变
    handleCurrentChange (val) {
      this.params.page = val
      this.getPropertyfeeList()
    },
    // 页容量发生改变
    handleSizeChange (val) {
      this.params.pageSize = val
      this.getPropertyfeeList()
    },
    // 处理序号
    indexMethod (index) {
      return (this.params.page - 1) * this.params.pageSize + index + 1
    },
    // 查询
    search () {
      this.params.page = 1
      if (this.valuedata) {
        this.params.start = this.valuedata[0]
        this.params.end = this.valuedata[1]
      }
      this.getPropertyfeeList()
    },
    // 获取所有企业列表
    async getAllEnterprise () {
      const res = await getAllEnterpriseAPI()
      this.enterpriseList = res.data
      // console.log('所有企业列表', this.enterpriseList)
    },
    // 获取所有楼宇
    async getAllBuilding () {
      const res = await getAllBuildingAPI()
      this.buildingList = res.data
      // console.log('获取所有楼宇', res.data)
    },
    // 计算物业费
    /* async getPayment (id) {
      // console.log('楼宇id',id)
      this.$refs.addForm.validateField('payTime', async valid => {
        if (!valid) {
          // 开始计算
          const data = {
            buildingId: id,
            startTime: this.addForm.payTime[0],
            endTime: this.addForm.payTime[1]
          }
          const res = await getPaymentAPI(data)
          this.addForm.paymentAmount = res.data
        }
      })
    }, */
    // 提交
    submit () {
      this.$refs.addForm.validate(async valid => {
        if (!valid) return
        // 开始提交 处理提交数据
        this.addForm.startTime = this.addForm.payTime[0]
        this.addForm.endTime = this.addForm.payTime[1]
        delete this.addForm.payTime // 删除 payTime 属性
        await addPaymentAPI(this.addForm)
        this.$message.success('添加成功') // 提示用户
        this.dialogVisible = false // 关闭弹窗
        this.$refs.addForm.resetFields() // 重置表单
        this.getPropertyfeeList() // 重新渲染列表
      })
    },
    // 删除
    async del (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await delPaymentAPI(id)
          this.$message.success('删除成功')
          this.getPropertyfeeList() // 重新渲染列表
        })
        .catch(() => {})
    },
    // 读取
    async read (data) {
      // console.log(data)
      this.readShow = true // 打开弹窗
      this.readData = data
    }
  },
  watch: {
    async 'addForm.payTime' (value) {
      console.log(value)
      if (this.addForm.buildingId) {
        // 计算
        const data = {
          buildingId: this.addForm.buildingId,
          startTime: value[0],
          endTime: value[1]
        }
        const res = await getPaymentAPI(data)
        this.addForm.paymentAmount = res.data
      }
    },
    async 'addForm.buildingId' (value) {
      if (this.addForm.payTime) {
        const data = {
          buildingId: value,
          startTime: this.addForm.payTime[0],
          endTime: this.addForm.payTime[1]
        }
        const res = await getPaymentAPI(data)
        this.addForm.paymentAmount = res.data
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.employee-container {
  padding: 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, 0.9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container {
  margin: 10px 0px;
}
.page-container {
  padding: 4px 0px;
  text-align: right;
}
.form-container {
  padding: 0px 80px;
}
</style>
